// /rule/addRuleInfo.do
var _url_ = '../goods/addGoodsInfo.do';
var _url_edit = '../goods/updateGoodsInfo.do';
var _url_list = '../goods/getGoodsList';
var _url_id = '../goods/getGoodsById';
var _url_stat = '../goods/updateGoodsStatus.do';
var _map = {};
///首页
var _pageNum = 1;
var _type ;
var spid="";
  $(function(){
	   
	   spid = getCookie("shop_id");
	  _type = getUrlParam("type");
	  console.log(_type)
	  if(_type === "add"){
		  getTypeListAdd();
		  $("#shopId").val(spid);
	  }else if(_type === "edit"){
		  getTypeListAdd();
		  $(".is_up_do").hide();
		  showEditData();
	  }else{
		  //获取分类列表
		  getTypeList();
		  //getTypeCount();
		  getList();
	  }
  })
$(function(){
	  $("body").on("click",".mypage span",function(){
		  _pageNum = $(this).attr("data-status");
		  getList();
	  })
	  $(".search-btn").click(function(){
			 var tit =  $("#goodsTitle").val();
			 var stts =  $("#status").val();
			 console.log(tit);
			 _map["goodsTitle"] = tit;
			 _pageNum = 1;
			 _map["status"] = stts;
			 getList();
		  });
	  $("body").on("click",".left-type li",function(){
			 var tt = $(this).text();
			 $(this).addClass('lt-li-active').siblings().removeClass('lt-li-active');
			 _map['goodsTypeId'] = $(this).attr('data-status');
			 _pageNum = 1 ;
			 getList();
	  });
});
  function getList(){
	  _map["pageNum"] = _pageNum;
	  _map["pageSize"] = 30;
	  _map['shopId'] = spid;
	  _ajax_get(_url_list,_map,function(res){
		  console.log(res.returnObject);
		  if(!res.returnObject||res.returnObject.length<=0 ){
			  $(".layui-table tbody").html("");
			  $(".no-more-data").show();
			  $(".x-right").text("共有数据：0 条")
			  showPageInfo(res.returnObject);
			  return;
		  }else{
			  $(".no-more-data").hide();
		  }
		  var html = "";
		  if(!res.returnObject){
			  html = '<div class="layui-form-mid layui-word-aux no-more-data">暂无更多数据</div>';
			  $(".layui-table ").after(html);
			  
			  return;
		  }
		  var stat = "" , statHtml = "";
		  var support = ""; //商家支持
		  showPageInfo(res.returnObject);
		  if($(".left-type ul li").eq(0).text()=='全部'){
			  $(".left-type ul li").eq(0).text('全部'+'('+res.returnObject.total+')');
		  }
		  $(".x-right").text("共有数据："+res.returnObject.total +" 条");
		  $.each(res.returnObject.list,function(i,v,arr){
			  
			  if(v.status == 1){
				  stat = "售卖中";
				  statHtml ='   <a title="下架" data-status="2"  onclick="member_ud(this,\''+v.id+'\')" href="javascript:;">                       '
 	             +'     <i class="iconfont icon">下架</i>                                                                                 '
	             +'   </a>';
			  }else if(v.status == 4){
				  stat="图片审核中";
				  statHtml = '';
			  }else{
				  stat= "下架";
				  statHtml ='   <a title="上架" data-status="1"  onclick="member_ud(this,\''+v.id+'\')" href="javascript:;">                       '
				  +'     <i class="iconfont ">上架</i>                                                                                 '
				  +'   </a>';
				  
			  }
			  if(v.goodsSupport){
				  var  sups = v.goodsSupport.split(',');
				  for (var k = 0; k < sups.length; k++) {
					  var sup = sups[k];
					  if(sup == 1){
						  support += "<span class=' shop-sup mei' >外卖</span>";
					  }else if(sup == 2){
						  support += "<span class=' shop-sup tang' >堂食</span>";
					  }
				  }
			  }else{
				  support = "-";
			  }
			  html+=' <tr><td>'
    	             +' <div class="layui-unselect layui-form-checkbox" lay-skin="primary" data-id="'+v.id+'" ><i class="layui-icon">&#xe605;</i></div>'
    	             +' </td>                                                                                                                  '
    	             +' <td>'
    	             +v.sort+'</td>                                                                                                             '
    	             +' <td>'+v.goodsTitle
    	             +'</td>                                                                                           '
    	             +' <td align="center"><div style="width:80px;height:80px "><img style="height:100%;width:100%"  src="'+v.goodsPic+'"  ></div></td>                                                                                                      '
    	             +' <td>'+support+'</td>                                                                                                      '
    	             +' <td>'+(v.priceOut/100).toFixed(2)+'</td>                                                                                                      '
    	             +' <td>'+(v.priceRoom/100).toFixed(2)+'</td>                                                                                                      '
    	             +' <td>'+v.goodsStock+'</td>                                                                                                      '
    	             +' <td>'+stat+'</td>                                                                                                      '
    	             +' <td class="td-manage">    '
    	             +'   <a title="编辑"  onclick="x_admin_show(\'修改菜品信息 - '+v.goodsTitle+'\',\'./goods-add.html?type=edit&id='+v.id+'\',550,580,true)" href="javascript:;">                       '
    	             +'     <i class="iconfont icon-">修改</i>                                                                                 '
    	             +'   </a>' +statHtml 
    	             + '<a title="删除" onclick="member_del(this,\''+v.id+'\')" href="javascript:;">                                         '
    	             +'     <i class="iconfont icon-delete x-red"></i>                                                                                 '
    	             +'   </a>  </td> </tr>';
			  support = "";
		  })
		  $(".layui-table tbody").html(html);
	  })
  }
  
  function getTypeList(){
	  
	  _ajax_post_token('../goods/getGoodsTypeList',{pageSize:100,shopId:spid},function(res){
		  var list =  res.returnObject;
		  var html = '<li data-status="" class="lt-li-active">全部</li>';;
		  $.each(list.list,function(i,v,arr){
    		  if(list){
    			 html+='<li data-status="'+v.id+'">'+v.title+'</li>';
    		  }
		  })
		  $(".left-type ul").html(html);
		  getTypeCount();
	  })
  }
  function getTypeCount(){
	  
	  _ajax_post_token('../goods/selectGoodsCountByTypeId.do',{shopId:spid},function(res){
		  console.log(res);
		  if(!res.returnObject){
			  return;
		  }
		  $.each(res.returnObject,function(i,v,arr){
			  var li = $(".left-type ul li");
			  for (var k = 0; k < li.length; k++) {
				  var lis = li[k];
				if(  $(lis).attr("data-status") == v.typeId  ){
					console.log();
					$(lis).text($(lis).text()+"("+v.COUNT+")");
				}
			}
		  })
	  })
  }
  
  
  function getTypeListAdd(){
	  console.log(spid);
	  _ajax_post_token('../goods/getGoodsTypeList',{pageSize:100,shopId:spid},function(res){
		  console.log(res);
		  var list =  res.returnObject;
		  var html = "";
		  $.each(list.list,function(i,v,arr){
    		  if(list){
    			 html+='<option value="'+v.id+'">'+v.title+'</option>';
    		  }
		  })
		  $(".pIdsel").append(html);
	  })
  }
  function showEditData(){
	  _url_ = _url_edit;
	  var _id = getUrlParam("id");
	  $("#paramKey").val(_id);
	  console.log(_id);
	  _ajax_post_token(_url_id,{userType:1,id:_id},function(res){
		  console.log(res);
		 var ddt = res.returnObject;
		 ddt.priceOut = (ddt.priceOut/100).toFixed(2);
		 ddt.priceRoom = (ddt.priceRoom/100).toFixed(2);
		 ddt.goodsPackAmount = (ddt.goodsPackAmount/100).toFixed(2);
		  _showData = ddt;
	  });
  }    
      
  /*用户-删除*/
  function member_del(obj,_id){
      layer.confirm('确认要删除吗？',function(index){
          //发异步删除数据
    	  _url_ =_url_stat;
    	  _ajax_post_token(_url_,{status:0,id:_id},function(res){
    	      	console.log(res);
    	      	if(res.resultCode == 1000){
    	      		getList();
    	      	  layer.msg('删除成功!',{icon:6,time:2000});
    	      	}else{
    	      		layer.msg('删除失败!',{icon:5,time:2000});
    	      	}
    	      });
      });
  }
  /*上下架*/
  function member_ud(obj,_id){
	  	  var stats = $(obj).attr('data-status');
		  //发异步删除数据
		  _url_ =_url_edit;
		  _ajax_post_token(_url_,{status:stats,id:_id},function(res){
			  console.log(res);
			  if(res.resultCode == 1000){
				  getList();
				  layer.msg('操作成功!',{icon: 6,time:1000});
			  }else{
				  layer.msg('操作失败!',{icon:5,time:2000});
			  }
		  });
  }
  //批量上架
  function member_udall(){
	  var data = tableCheck.getData();
      if(!data || data.length==0){
    	  layer.msg('至少选择一条数据',{icon:5,time:2000});
    	  return;
      }
	  _ajax_post_token(_url_stat,{status:1,ids:data.join(',')},function(res){
		  console.log(res);
		  if(res.resultCode == 1000){
			  getList();
			  layer.msg('操作成功!',{icon: 6,time:1000});
		  }else{
			  layer.msg('操作失败!',{icon:5,time:2000});
		  }
	  });
  }
  function member_udall2(){
	  var data = tableCheck.getData();
      if(!data || data.length==0){
    	  layer.msg('至少选择一条数据',{icon:5,time:2000});
    	  return;
      }
	  data = tableCheck.getData().join(',');
	  _ajax_post_token(_url_stat,{status:2,ids:data},function(res){
		  console.log(res);
		  if(res.resultCode == 1000){
			  getList();
			  layer.msg('操作成功!',{icon: 1,time:1000});
		  }else{
			  layer.msg('操作失败!',{icon:5,time:2000});
		  }
	  });
  }
  
  
  /*用户-启动*/
  function member_start(obj,_id){
	  layer.confirm('确认要解封此账号吗？',function(index){
		  //发异步删除数据
		  _url_ =_url_edit;
		  _ajax_post_token(_url_,{userStatus:1,id:_id},function(res){
			  console.log(res);
			  if(res.resultCode == 1000){
				  getList();
				  layer.msg('已解封!',{icon: 6,time:1000});
			  }else if (res.resultCode == 1008){
				  layer.msg('当前账号已存在',{icon:5,time:2000});
			  }
			  else{
				  layer.msg('操作失败!',{icon:5,time:2000});
			  }
		  });
	  });
  }
      
  function updateInfo(data){
	  //发异步，把数据提交给后台
	disAddBtn();
	 console.log(_url_);
      _ajax_post_token(_url_,data,function(res){
      	console.log(res);
      	if(res.resultCode == 1000){
      	  parent.getList();
      	  parent.layer.msg('操作成功!',{icon:1,time:2000});
      	  x_admin_close();
          // 获得frame索引
      	}else{
      		layer.msg('操作失败!',{icon:1,time:2000});
      	}
      	celDisAddBtn();
      });
  }
      
      
  function delAll (argument) {
      var data = tableCheck.getData();
      if(!data || data.length==0){
    	  layer.msg('至少选择一条数据',{icon:5,time:2000});
    	  return;
      }
      console.log(data.join(","));
      layer.confirm('确认要删除已选择的数据吗？',function(index){
          //捉到所有被选中的，发异步进行删除
    	  _ajax_post_token(_url_stat,{ids:data.join(","),status:0},function(res){
    	      	console.log(res);
    	      	if(res.resultCode == 1000){
    	      	  getList();
    	      	  layer.msg('操作成功!',{icon:1,time:2000});
    	          // 获得frame索引
    	      	}else{
    	      		layer.msg('操作失败!',{icon:1,time:2000});
    	      	}
    	      });
      });
    }      
       
      
function downloadCityExcel(){
	window.open('../excel/cityDownExcel.do');
}   
function uploadGoods(){
	
}      
      
//添加 和 修改窗口     